<%@page import="com.wiseMen.platform.support.security.UserUtils"%>
<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<form id="search_${uuid}" style="display: none;" class="box-body form-float"></form>
<div class="row">
	<div class="col-md-12"  >
		<div class="box box-body">
			<form name="kbmsSearchForm" method="post">
			<div class="input-group col-lg-6">
				 <input type="hidden" name="perSearchText"/>
                 <input type="text" id="txt_${uuid}" name="searchText" class="form-control">
                 <span class="input-group-btn">
                     <button class="btn btn-default" onclick="kbms_searcher(1)" type="button">搜索</button>
                 </span>
            </div>
            </form>
            <br/>
            <div>
				<ul id="searcherList_${uuid }" class="media-list">
				</ul>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
$(function(){
    $( "#txt_${uuid}" ).autocomplete({
    	source: function(request, response ) {
            $.ajax({
                url: ctx + "/KBMS/Keywork/search.do",
                dataType: "json",
                data:{
                	keywork: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return item;
                    }));
                }
            });
        },
        minLength:1,
        delay:1000,
        select: function( event, ui ) {},
        change: function( event, ui ) {}
    });
});
function kbms_searcher(pageNum){
	var searchText = $("form[name='kbmsSearchForm'] input[name='searchText']").val();
	searcherSbumit(pageNum,searchText);
	$("form[name='kbmsSearchForm'] input[name='perSearchText']").val(searchText);
}

function pageSearcher(pageNum){
	var searchText = $("form[name='kbmsSearchForm'] input[name='perSearchText']").val();
	searcherSbumit(pageNum,searchText);
}

function searcherSbumit(pageNum,searchText){
	if($.trim(searchText)!=''){
		$.post(ctx+"/KBMS/searcher/search.do",{
			searchText:searchText,
			pageNum:pageNum,
			pageSize:30
		},function(data){
			if(data.success){
				data = data.obj;
				var list = $("#searcherList_${uuid }");
				if(data.dataSize>0){
					list.empty();
					for(var i=0; i<data.data.length; i++){
						var entity = data.data[i];
						list.append(getSearcherListItems().format(entity.title,entity.contentStr.substring(0,200)));
					}
					if(data.totalPage>1){
						var pagination = "<li><nav class='text-center'><ul class='pagination'><li><a href='javascript:pageSearcher(1)'>&laquo;</a></li>";
						for(var i=1; i<=data.pageRange.length; i++){
							pagination += "<li class='{0}'><a href='javascript:pageSearcher("+i+")'>"+i+"</a></li>";
							if(data.pageNum==i){
								pagination = pagination.format("active");
							}else{
								pagination = pagination.format("");
							}
						}
						pagination += "</li><li><a href='javascript:pageSearcher("+data.totalPage+")'>&raquo;</a></li></ul></nav></li>";
						list.append(pagination);
					}
				}else{
					list.empty();
					list.append(getSearcherListItems().format("","<div style='color:#ccc;'>没有找到相关数据。</div>"));
				}
			}else{
				
			}
		},'json');
	}
}

function getSearcherListItems(){
	return "<li class='media'>"+
				"<div class='media-body'>"+
					"<h4 class='media-heading'>{0}</h4>{1}"+
			"</div>"+
		"</li>";
}
</script>